﻿namespace TfsCloudDeploy.Workflow.Core
{
    using System;
    using System.ComponentModel;
    using System.Text;
    using TfsCloudDeploy.Workflow.Core.Activities;

    /// <summary>
    /// Settings pertaining to applying transform to configuration files in order to generate a resulting configuration file
    /// that is appropriate for the target deployment environment.
    /// </summary>
    [Serializable, TypeConverter(typeof(ConfigurationTransformationSettingsConverter))]
    public class ConfigurationTransformationSettings
    {
        public ConfigurationTransformationSettings()
        {

        }

        public ConfigurationTransformationSettings(bool applyTransformToConfigurationFiles, string configurationFilesHost, string configurationFilesPath, ConfigurationSystemType transformConfigurationSystemType)
            : this()
        {
            this.ApplyTransformToConfigurationFiles = applyTransformToConfigurationFiles;
            this.ConfigurationFilesHost = configurationFilesHost;
            this.ConfigurationFilesPath = configurationFilesPath;
            this.TransformConfigurationSystemType = transformConfigurationSystemType;
        }

        /// <summary>
        /// Set to true to have transforms applied to the configuration files.
        /// </summary>
        [Browsable(true), RefreshProperties(RefreshProperties.All), DefaultValue(false), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.ApplyTransformToConfigurationFilesDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.ApplyTransformToConfigurationFilesDescription)]
        public bool ApplyTransformToConfigurationFiles { get; set; }

        /// <summary>
        /// Set to the path to the configuration files host.
        /// </summary>
        [Browsable(true), RefreshProperties(RefreshProperties.All), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.ConfigurationFilesHostDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.ConfigurationFilesHostDescription)]
        public string ConfigurationFilesHost { get; set; }

        /// <summary>
        /// Set to the root path to the configuration files on the configuration files host.
        /// </summary>
        [Browsable(true), RefreshProperties(RefreshProperties.All), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.ConfigurationFilesPathDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.ConfigurationFilesPathDescription)]
        public string ConfigurationFilesPath { get; set; }

        /// <summary>
        /// Set to the type of the configuration system storing configuration files.
        /// </summary>
        [Browsable(true), RefreshProperties(RefreshProperties.All), DefaultValue(ConfigurationSystemType.TFS), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.TransformConfigurationSystemTypeDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.TransformConfigurationSystemTypeDescription)]
        public ConfigurationSystemType TransformConfigurationSystemType { get; set; }

        /// <summary>
        /// The source control path to the transform script (e.g.: $/TFS/Configuration/Scripts/TransformConfig.targets).
        /// </summary>
        [Browsable(true), RefreshProperties(RefreshProperties.All), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.TransformScriptPathDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.TransformScriptPathDescription)]
        public string TransformScriptPath { get; set; }

        [Browsable(true), RefreshProperties(RefreshProperties.All), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.TargetTransformsDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.TargetTransformsDescription)]
        public TargetTransform[] TargetTransforms { get; set; }

        [Browsable(false)]
        public bool HasConfigurationFilesHost
        {
            get
            {
                return !string.IsNullOrWhiteSpace(this.ConfigurationFilesHost);
            }
        }

        [Browsable(false)]
        public bool HasConfigurationFilesPath
        {
            get
            {
                return !string.IsNullOrWhiteSpace(this.ConfigurationFilesPath);
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            bool hasConfigurationFilesHost = this.HasConfigurationFilesHost;

            if (this.ApplyTransformToConfigurationFiles && hasConfigurationFilesHost)
            {
                builder.Append("Apply Configuration Transforms");
            }

            if (hasConfigurationFilesHost)
            {
                builder.AppendFormat(" Using {0} [{1}", this.TransformConfigurationSystemType, this.ConfigurationFilesHost);

                if (this.HasConfigurationFilesPath)
                {
                    builder.AppendFormat(":{0}", this.ConfigurationFilesPath);
                }

                builder.Append("]");
            }

            string configurationSettingsDisplayText = builder.ToString();

            return string.IsNullOrWhiteSpace(configurationSettingsDisplayText) ? "Not Configured" : configurationSettingsDisplayText;
        }
    }
}
